﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CUILoader;
using System.IO;
using System.Xml;

namespace LabelCUI
{
    public class CLabel : ICUI
    {
        public List<CAttribute> lstAttributes;
        public string strName;
        public CLabel()
        {
            strName = "Label";
            lstAttributes = new List<CAttribute>();
            CAttribute newAttribute = new CAttribute();
            newAttribute.StrName = "Name";
            newAttribute.StrValue = "";
            newAttribute.ValueType = eType.String;
            lstAttributes.Add(newAttribute);

            newAttribute = new CAttribute();
            newAttribute.StrName = "Text";
            newAttribute.StrValue = "";
            newAttribute.ValueType = eType.String;
            lstAttributes.Add(newAttribute);

            newAttribute = new CAttribute();
            newAttribute.StrName = "FontStyle";
            newAttribute.StrValue = eFontStyle.Plain.ToString();
            newAttribute.ValueType = eType.FontStyle;
            lstAttributes.Add(newAttribute);

            newAttribute = new CAttribute();
            newAttribute.StrName = "FontSize";
            newAttribute.StrValue = eFontSize.Small.ToString();
            newAttribute.ValueType = eType.FontSize;
            lstAttributes.Add(newAttribute);

            newAttribute = new CAttribute();
            newAttribute.StrName = "ForeColor";
            newAttribute.StrValue = "";
            newAttribute.ValueType = eType.String;
            lstAttributes.Add(newAttribute);


        }
        public void toXML()
        {
            string file = strName + ".xml";
            FileStream fs = new FileStream(file, FileMode.Create);
            XmlTextWriter w = new XmlTextWriter(fs, null);
            w.WriteStartDocument();

            w.WriteStartElement(strName);

            w.WriteComment("This xml file generated for " + strName + " UI");

            foreach (CAttribute item in lstAttributes)
            {
                w.WriteStartElement(item.StrName);
                w.WriteAttributeString("Value", item.StrValue);
                w.WriteAttributeString("ValueType", item.ValueType.ToString());
                w.WriteEndElement();
            }

            // Close the root element.
            w.WriteEndElement();

            w.WriteEndDocument();
            w.Close();
        }

        public void fromXML()
        {
            string file = strName + ".xml";
            FileStream fs = new FileStream(file, FileMode.Open);
            XmlTextReader r = new XmlTextReader(fs);

            while (r.Read())
            {
                if (r.NodeType == XmlNodeType.Element)
                {
                    foreach (CAttribute item in lstAttributes)
	                {
                        if (r.Name == item.StrName)
                        {
                            item.StrValue = r.GetAttribute(0);
                            item.ValueType = (eType)Enum.Parse(typeof(eType), r.GetAttribute(1), true);
                        }
	                }
                }
            }
            fs.Close();

        }

        public List<CAttribute> getAttributes()
        {
            return lstAttributes;
        }

        public string getName()
        {
            return strName;
        }
    }
}
